/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.navigation.find;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import ch.bfh.aeneas.R;
import ch.bfh.aeneas.navigation.app.NavigationActivity;
import ch.bfh.aeneas.navigation.app.NavigationService.NavigationServiceBinder;

/**
 * FindMe tries to get a human readable address as close as possible to the
 * current location.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class FindMe extends NavigationActivity {

    protected ServiceConnection mFindConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            mFindService = (FindService) ((NavigationServiceBinder) arg1).getService();
            mFindService.addOnResultsListener(mResultsListener);
            mFindStarted = true;
            FindMe.super.startListening();
        }

        public void onServiceDisconnected(ComponentName arg0) {
            stopListening();
        }
    };
    
    private String LOG_TAG = "FindMe";
    private boolean mFindStarted;
    private FindService mFindService;
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle, R.layout.findme);
        mFindStarted = false;
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onResume() {
        super.onResume();
        
        bindService(new Intent(this, FindService.class),mFindConnection,Context.BIND_AUTO_CREATE);
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onPause() {
        super.onPause();
        
        unbindService(mFindConnection);
    }

    @Override
    /**
     * {@inheritDoc}
     */
    protected void onPositionReceivedFirst(Location l) {
        Log.i(LOG_TAG, "Position received!");
        mFindService.findMe(l);
        super.stopListening();
    }

    @Override
    /**
     * {@inheritDoc}
     */
    protected void onRequestFinished(int request) {
        Log.i(LOG_TAG, "Request finished");
        outputAnswers();
    }

    private void outputAnswers() {
        Intent intent;
        Log.i(LOG_TAG, "Received "+mAnswers.size()+" answers!");
        for(int i = 0; i < mAnswers.size(); i++) {
            intent = mAnswers.get(i);
            if(intent.hasExtra("address")) {
                Log.i(LOG_TAG, "Found address: "+intent.getStringExtra("address"));
                speech(getString(R.string.find_pre_adress));
                speechAiOn(intent.getStringExtra("address"));
            }
        }
    }
}